﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Cartoon.Skin;

namespace CartoonViewer.Controls
{
    /// <summary>
    /// BaseWindow.xaml 的交互逻辑
    /// </summary>
    public partial class BaseWindow : Window
    {
        protected ISkinService SkinService;
        
        public BaseWindow()
        {
            InitializeComponent(); 

            SkinService = new SkinService();
            SkinService.Changed += SkinService_Changed;

            this.WindowStyle = WindowStyle.None;

            CvsTitle.MouseLeftButtonDown += CvsTitle_MouseLeftButtonDown;
            CvsTitle.MouseMove += CvsTitle_MouseMove;
            CvsTitle.MouseLeftButtonUp += CvsTitle_MouseLeftButtonUp;

            WinBtnClose.Click += WinBtnClose_Click;
            WinBtnMin.Click += WinBtnMin_Click;
        }

        private void SkinService_Changed()
        {
            this.LoadBackGround();
        }

        private ImageBrush mainBackGround;
        private void LoadBackGround()
        {
            ImageBrush b = new ImageBrush();
            b.ImageSource = this.SkinService.Skin.BackGround;

            mainBackGround = b;
            this.Win.Background = mainBackGround;
        }

        

        #region 标题按扭事件
        
        private void WinBtnMin_Click(object sender, RoutedEventArgs e)
        {
            this.WindowState = WindowState.Minimized;
        }

        private void WinBtnClose_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        #endregion

        #region 移动窗口

        bool IsWinMoving = false;
        Point pbegin;
        Point pend;

        private void CvsTitle_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (this.PlTitleBtn.IsMouseOver) return;

            this.pbegin = e.GetPosition(this);
            this.IsWinMoving = this.CvsTitle.CaptureMouse();
            this.IsWinMoving = true;
        }

        private void CvsTitle_MouseMove(object sender, MouseEventArgs e)
        {
            if (this.IsWinMoving == false) return;

            this.pend = e.GetPosition(this);

            if (pbegin.X == pend.X && pbegin.Y == pend.Y) return;

            double moveX, moveY;

            moveX = pend.X - pbegin.X;
            moveY = pend.Y - pbegin.Y;

            this.Left += moveX;
            this.Top += moveY;
        }

        private void CvsTitle_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            this.IsWinMoving = false;
            this.CvsTitle.ReleaseMouseCapture();
        }

        #endregion

        public new static DependencyProperty TitleProperty;
        public new string Title
        {
            get 
            {
                return this.LbTitle.Text;
            }
            set
            {
                this.LbTitle.Text = value;
            }
        }

        public static DependencyProperty MyContentProperty;

        private Panel _content;
        public Panel MyContent
        {
            get
            {
                return _content;
            }
            set 
            {
                var newContent = value;
                if (newContent != _content)
                {
                    _content = newContent;
                    this.gContent.Children.Clear();
                    this.gContent.Children.Add(_content);
                }
            }
        }

    }
}
